package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.ui.adapter.SimpleFragmentPagerAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateActivity;
import com.chain.BrotherExchange.ui.fragment.MyTransactionOrderFragment;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.viewmodel.LegalTenderDealViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.Bind;

/**
 * 我的交易单
 */
public class MyTransactionOrderActivity extends AbsNetworkStateActivity {

    @Bind(R.id.total_deal_order_text) TextView totalDealOrderText;
    @Bind(R.id.th_deal_order_text) TextView thDealOrderText;
    @Bind(R.id.completion_rate_text) TextView completionRateText;
    @Bind(R.id.appeal_text) TextView appealText;
    @Bind(R.id.winning_text) TextView winningText;
    @Bind(R.id.average_release_text) TextView averageReleaseText;
    @Bind(R.id.real_name_authentication_layout) LinearLayout realNameAuthenticationLayout;
    @Bind(R.id.level_2_certification_layout) LinearLayout level2CertificationLayout;
    @Bind(R.id.phone_authentication_layout) LinearLayout phoneAuthenticationLayout;
    @Bind(R.id.tab_layout) TabLayout tabLayout;
    @Bind(R.id.user_deal_details_view_pager) ViewPager userDealDetailsViewPager;

    private LegalTenderDealViewModel mViewModel;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, MyTransactionOrderActivity.class));
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_my_transaction_order;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.my_transaction_order);
        mViewModel = ViewModelProviders.of(this).get(LegalTenderDealViewModel.class);

        initUserInfo();

        initUserDealList();
    }

    private void initUserDealList() {
        tabLayout.setupWithViewPager(userDealDetailsViewPager);
        userDealDetailsViewPager.setAdapter(new SimpleFragmentPagerAdapter(
                getSupportFragmentManager(),
                new ArrayList<>(Arrays.asList(
                        MyTransactionOrderFragment.newInstance(1),
                        MyTransactionOrderFragment.newInstance(2)
                )),
                new ArrayList<>(
                        Arrays.asList(getString(R.string.buy), getString(R.string.sell))
                )));
    }

    private void initUserInfo() {
        mViewModel.legalTenderUserInfo(Androids.LocalConfig.getUid()).observe(this, resource -> {
            Resource.load(this, resource, this, result -> {
                completionRateText.setText(result.getComplete_rate() + "%"); // 完成率
                appealText.setText(result.getAppeal() + getString(R.string.text_times)); // 申诉
                winningText.setText(result.getSuccess() + getString(R.string.text_times)); // 胜诉
                totalDealOrderText.setText(result.getOrders() + getString(R.string.text_times)); // 总成单
                thDealOrderText.setText(result.getMonth_rate() + getString(R.string.text_times)); // 30成单
                averageReleaseText.setText(result.getAverage_to() + getString(R.string.text_times)); // 平均放行

                if (result.getPhone_auth() == 1) {
                    phoneAuthenticationLayout.setVisibility(View.VISIBLE);
                }

                if (result.getReal_name() == 1) {
                    realNameAuthenticationLayout.setVisibility(View.VISIBLE);
                }

                if (result.getTwo_level_auth() == 1) {
                    level2CertificationLayout.setVisibility(View.VISIBLE);
                }
            });
        });
    }

    @Override
    public void onAnewRequest() {
        initUserInfo();
    }
}
